
#include <stdlib.h>
#include <stdio.h>

#include "aecm/echo_control_mobile.h"
#include "ns/noise_suppression.h"
#include "ns/noise_suppression_x.h"

#define FRAMEINT16 160
#define FRAMEBYTE (160*2)

#if 0
int main(int argc, char *argv[])
{
    FILE *fidnear = fopen(argv[1], "rb");
	fseek(fidnear, 0, SEEK_END); int d2 = ftell(fidnear);
    fseek(fidnear, 0, SEEK_SET); int d1 = ftell(fidnear);
    int nframes = (d2-d1)/FRAMEBYTE;
    printf("total frames = %d\n", nframes);

    FILE *fidfar = fopen(argv[2], "rb");
    FILE *fidaec = fopen(argv[3], "wb");
    short micbuf[FRAMEINT16] = {0};
    short refbuf[FRAMEINT16] = {0};
    short aecbuf[FRAMEINT16] = {0};

    void *aecmInst = WebRtcAecm_Create();
    WebRtcAecm_Init(aecmInst, 16000);

    AecmConfig config;
    config.cngMode = AecmTrue;
    config.echoMode = 0; /*0,1,2,3,4*/
    WebRtcAecm_set_config(aecmInst, config);

    for (int n=0; n<nframes; n++) {
        fread(micbuf, sizeof(short), FRAMEINT16, fidnear);
        fread(refbuf, sizeof(short), FRAMEINT16, fidfar);
        WebRtcAecm_BufferFarend(aecmInst, refbuf, FRAMEINT16);
        WebRtcAecm_Process(aecmInst, micbuf, NULL, aecbuf, FRAMEINT16, 1);
        fwrite(aecbuf, sizeof(short), FRAMEINT16, fidaec);
    }

    WebRtcAecm_Free(aecmInst);
    fclose(fidnear); fclose(fidfar); fclose(fidaec);

    return 0;
}
#endif

#if 0
int main(int argc, char *argv[])
{
    FILE *fidnear = fopen(argv[1], "rb");
	fseek(fidnear, 0, SEEK_END); int d2 = ftell(fidnear);
    fseek(fidnear, 0, SEEK_SET); int d1 = ftell(fidnear);
    int nframes = (d2-d1)/FRAMEBYTE;
    printf("total frames = %d\n", nframes);
    short micbuf[FRAMEINT16] = {0};
    float xbuf[FRAMEINT16] = {0.0f};
    float *micband[3] = {xbuf, NULL, NULL};
    float ybuf[FRAMEINT16] = {0.0f};
    float *outband[3] = {ybuf, NULL, NULL};
    FILE *fidaec = fopen(argv[2], "wb");

    NsHandle *nsInst = WebRtcNs_Create();
    WebRtcNs_Init(nsInst, 16000);
    WebRtcNs_set_policy(nsInst, 2);

    for (int n=0; n<nframes; n++) {
        fread(micbuf, sizeof(short), FRAMEINT16, fidnear);
        for (int i=0; i<FRAMEINT16; i++) xbuf[i] = (float)micbuf[i];
        WebRtcNs_Analyze(nsInst, xbuf);
        WebRtcNs_Process(nsInst, micband, 1, outband);
        for (int i=0; i<FRAMEINT16; i++) micbuf[i] = (short)ybuf[i];
        fwrite(micbuf, sizeof(short), FRAMEINT16, fidaec);
    }
    WebRtcNs_Free(nsInst);

    fclose(fidnear); fclose(fidaec);
    return 0;
}
#endif

int main(int argc, char *argv[])
{
    FILE *fidnear = fopen(argv[1], "rb");
	fseek(fidnear, 0, SEEK_END); int d2 = ftell(fidnear);
    fseek(fidnear, 0, SEEK_SET); int d1 = ftell(fidnear);
    int nframes = (d2-d1)/FRAMEBYTE;
    printf("total frames = %d\n", nframes);
    short micbuf[FRAMEINT16] = {0};    
    short *micband[3] = {micbuf, NULL, NULL};

    short ybuf[FRAMEINT16] = {0};
    short *outband[3] = {ybuf, NULL, NULL};
    FILE *fidaec = fopen(argv[2], "wb");

    NsxHandle *nsInst = WebRtcNsx_Create();
    WebRtcNsx_Init(nsInst, 16000);
    WebRtcNsx_set_policy(nsInst, 2);

    for (int n=0; n<nframes; n++) {
        fread(micbuf, sizeof(short), FRAMEINT16, fidnear);        
        WebRtcNsx_Process(nsInst, micband, 1, outband);        
        fwrite(ybuf, sizeof(short), FRAMEINT16, fidaec);
    }
    WebRtcNsx_Free(nsInst);

    fclose(fidnear); fclose(fidaec);
    return 0;
}